package com.axzhengxin.platform.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.axzhengxin.platform.business.domain.BizPledgeInfo;
import com.axzhengxin.platform.business.service.IBizPledgeInfoService;
import com.axzhengxin.platform.common.util.SmsSendService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.Size;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author AHZJ0005 抵押Controller
 */
@Slf4j
@RestController
@RequestMapping("/pledge")
public class BizPledgeController extends BaseController {


    @Autowired
    private IBizPledgeInfoService bizPledgeInfoService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private SmsSendService smsSendService;
    /**
     * 抵押创建
     */
    private String pledgeCreate = "PLEDGE_CREATE";
    /**
     * 抵押解除
     */
    private String pledgeDisable = "PLEDGE_DISABLE";

    /**
     * 抵押文件上传
     *
     * @param file 文件
     * @return 路径
     */
    @PreAuthorize("@ss.hasPermi('pledge:uploadFile')")
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(MultipartFile file) {
        try {
            String upload = FileUploadUtils.upload(file);
            return AjaxResult.success(upload);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return AjaxResult.error("文件上传失败");
    }

    /**
     * 抵押申请
     *
     * @param bizPledgeInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('pledge:createPledge')")
    @PostMapping("/createPledge")
    public AjaxResult applyPledge(BizPledgeInfo bizPledgeInfo) {
        return bizPledgeInfoService.applyPledge(bizPledgeInfo);
    }

    /**
     * 解押操作
     *
     * @param flowUid 流程ID
     * @return
     */
    @PreAuthorize("@ss.hasPermi('pledge:disablePledge')")
    @PostMapping("/disablePledge")
    public AjaxResult disablePledge(String flowUid, String phoneCode, String phone) {
        return bizPledgeInfoService.disablePledge(flowUid, phoneCode, phone);
    }


    /**
     * 获取手机验证码 需要与预留手机号匹配
     *
     * @param phone 手机号
     * @return
     */
    @PreAuthorize("@ss.hasPermi('pledge:getPhoneCode')")
    @PostMapping("/getPhoneCode")
    public AjaxResult getPhoneCode(String phone, String businessType) {
        String phonenumber = SecurityUtils.getLoginUser().getUser().getPhonenumber();
        if (StringUtils.isNotEmpty(phone)) {
            if (phonenumber.equals(phone)) {
                String code = SmsSendService.randomCode(5);
                if (StringUtils.isNotEmpty(code)) {
                    redisCache.setCacheObject(phone + businessType, code, 1, TimeUnit.MINUTES);
                    String resultCode = smsSendService.sendMsg("prod", phone, null, "您正在执行解押操作，你的验证码是" + code + ",有效期一分钟，请妥善保管！", "412607");
                    /* if ("0".equals(redisCode)) {*/
                    log.info(code + "-------------------解押验证码----------------");
                    /* }*/
                    return AjaxResult.success("验证码获取成功，请查收！");
                }
                return AjaxResult.error("业务类型传入有误！");
            } else {
                return AjaxResult.error("请使用预留手机号办理业务！");
            }
        }
        return AjaxResult.error("手机号必填");
    }
}
